package cn.xuedeng.edu;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * mp3.5.5全局统一代码生成器
 *
 * @author zjx
 * @since 2024/3/7
 */
@Slf4j
class CodeGenerator {

    //代码默认注释作者
    static final String author = "ll";//作者

    static final String dataSourceUrl =
        "jdbc:mysql://192.168.1.48:3306/cloud_blackboard?useUnicode=true&characterEncoding=UTF-8&relaxAutoCommit=true"
            + "&zeroDateTimeBehavior=convertToNull";

    static final String driverName = "com.mysql.cj.jdbc.Driver";

    static final String dataSourceUserName = "";

    static final String dataSourcePassWord = "";

    static final String modelName = "elearn/cloud-system";//模块名称

    static final String packageName = "com.yizhi.cloud.blackboard.service";//包名

    static final String path = "D:\\projects\\wmy-parent\\cloud-blackboard\\src\\main\\";// 绝对路径，到\src\main\

    private static List<IFill> makeInitTableFills() {
        List<IFill> tableFillList = new ArrayList<>(6);
        /*定义创建时间 插入时默认填充**/

        tableFillList.add(new Column("create_time", FieldFill.INSERT));
        tableFillList.add(new Column("create_by_id", FieldFill.INSERT));
        tableFillList.add(new Column("create_by_name", FieldFill.INSERT));
        tableFillList.add(new Column("update_by_id", FieldFill.INSERT_UPDATE));
        tableFillList.add(new Column("update_by_name", FieldFill.INSERT_UPDATE));
        tableFillList.add(new Column("update_time", FieldFill.INSERT_UPDATE));

        return tableFillList;
    }

    @Test
    void test() {
        FastAutoGenerator.create(dataSourceUrl, dataSourceUserName, dataSourcePassWord)
            .globalConfig(builder -> builder.author(author).dateType(DateType.ONLY_DATE)
                //启用swagger
                .enableSwagger()
                //结束时是否打开文件夹 默认false
                .disableOpenDir()
                //指定输出目录
                .outputDir(path + "java\\")).packageConfig(builder -> builder
                //父包名。如果为空，将下面子包名必须写全部， 否则就只需写子包名
                .parent(packageName).entity("domain.testing")//实体类包名
                .controller("controller.testing")//控制层包名
                .mapper("mapper.testing")//mapper层包名
                //.other("dto")//生成dto目录 可不用
                .service("service.testing")//service层包名
                .serviceImpl("service.impl.testing")//service实现类包名
                //自定义mapper.xml文件输出目录
                .pathInfo(Collections.singletonMap(OutputFile.xml, path + "resources\\mybatis"))).strategyConfig(builder ->
                //设置要生成的表名
                builder.addInclude("testing_rating_visible").entityBuilder().enableFileOverride().enableLombok()
                    //开启Lombok模型
                    .enableRemoveIsPrefix().addTableFills(makeInitTableFills())
                    .enableTableFieldAnnotation()//开启生成实体时生成字段注解 @TableField("")
                    .logicDeleteColumnName("deleted").enableChainModel()//开启链式模型 @Accessors(chain = true)
                    .naming(NamingStrategy.underline_to_camel)//数据表映射实体命名策略：默认下划线转驼峰underline_to_camel
                    .columnNaming(NamingStrategy.underline_to_camel)//表字段映射实体属性命名规则：默认null，不指定按照naming执行
                    .formatFileName("%s")//格式化实体名称，%s取消首字母I,
                    .mapperBuilder().enableFileOverride()//.mapperAnnotation(Mapper.class)//开启mapper注解
                    .enableBaseResultMap()//启用xml文件中的BaseResultMap 生成
                    .enableBaseColumnList()//启用xml文件中的BaseColumnList
                    .formatMapperFileName("%sMapper")//格式化Dao类名称
                    .formatXmlFileName("%sMapper")//格式化xml文件名称
                    .serviceBuilder().enableFileOverride().formatServiceFileName("%sService")//格式化 service 接口文件名称
                    .formatServiceImplFileName("%sServiceImpl")//格式化 service 接口文件名称
                    .controllerBuilder().enableFileOverride().enableRestStyle())
            .dataSourceConfig(v -> v.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                // 兼容旧版本转换成Integer
                if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
                    return DbColumnType.INTEGER;
                }
                return typeRegistry.getColumnType(metaInfo);
            })).execute();
    }
}
